package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SpeciesUserMapper;
import com.ruoyi.common.core.domain.entity.SpeciesUser;
import com.ruoyi.system.service.ISpeciesUserService;

/**
 * 用户Service业务层处理
 *
 * @author DRIES
 * @date 2022-03-09
 */
@Service
public class SpeciesUserServiceImpl implements ISpeciesUserService
{
    @Autowired
    private SpeciesUserMapper speciesUserMapper;


    /**
     * 查询用户
     *
     * @param id 用户主键
     * @return 用户
     */
    @Override
    public SpeciesUser selectSpeciesUserById(Long id)
    {
        return speciesUserMapper.selectSpeciesUserById(id);
    }

    /**
     * 查询用户列表
     *
     * @param speciesUser 用户
     * @return 用户
     */
    @Override
    public List<SpeciesUser> selectSpeciesUserList(SpeciesUser speciesUser)
    {
        return speciesUserMapper.selectSpeciesUserList(speciesUser);
    }

    /**
     * 新增用户
     *
     * @param speciesUser 用户
     * @return 结果
     */
    @Override
    public int insertSpeciesUser(SpeciesUser speciesUser)
    {
        speciesUser.setCreateTime(DateUtils.getNowDate());
        return speciesUserMapper.insertSpeciesUser(speciesUser);
    }

    /**
     * 修改用户
     *
     * @param speciesUser 用户
     * @return 结果
     */
    @Override
    public int updateSpeciesUser(SpeciesUser speciesUser)
    {
        speciesUser.setUpdateTime(DateUtils.getNowDate());
        return speciesUserMapper.updateSpeciesUser(speciesUser);
    }

    /**
     * 批量删除用户
     *
     * @param ids 需要删除的用户主键
     * @return 结果
     */
    @Override
    public int deleteSpeciesUserByIds(Long[] ids)
    {
        return speciesUserMapper.deleteSpeciesUserByIds(ids);
    }

    /**
     * 删除用户信息
     *
     * @param id 用户主键
     * @return 结果
     */
    @Override
    public int deleteSpeciesUserById(Long id)
    {
        return speciesUserMapper.deleteSpeciesUserById(id);
    }

    /**
     * 查询用户
     *
     * @param openId 用户主键
     * @return 用户
     */
    @Override
    public SpeciesUser selectSpeciesUserByOpenId(String openId)
    {
        return speciesUserMapper.selectSpeciesUserByOpenId(openId);
    }


    @Override
    public SpeciesUser loginByWxMiniApp(WxMaUserInfo userInfo) {

        // 查询openId
        SpeciesUser speciesUser = this.selectSpeciesUserByOpenId(userInfo.getOpenId());
        if (ObjectUtil.isNotNull(speciesUser)) {
            return speciesUser;
        } else {
            SpeciesUser user = new SpeciesUser();
            user.setAccount(userInfo.getOpenId());
            user.setNickname(userInfo.getNickName());
            user.setAvatar(userInfo.getAvatarUrl());
            user.setOpenId(userInfo.getOpenId());
            user.setIntegral(0L);
            user.setCreateBy("微信注册用户");
            user.setCreateTime(new Date());
            user.setRemark("");

            int i = speciesUserMapper.insertSpeciesUser(user);
            if (i == 1) {
                return user;
            }
        }

        return null;
    }
}
